<template>
  <div class="loginBox">
    <div class="userInpnut">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="name">
        <el-input
          type="text"
          maxlength="20"
          v-model="ruleForm.name"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input
          type="password"
          maxlength="20"
          v-model="ruleForm.pass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="登录权限" prop="power">
        <el-radio-group v-model="ruleForm.power">
          <el-radio label="普通用户"></el-radio>
          <el-radio label="管理员"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button
          class="loginButton"
          type="primary"
          @click="submitForm('ruleForm', ruleForm)"
          >登录</el-button
        >
        <router-link to="/register">
          <el-button class="loginButton" type="success">注册</el-button>
        </router-link>
        <router-link to="/airplane">
          <el-button class="loginButton">主页</el-button>
        </router-link>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>
<script>
export default {
  name: "Register",
  data() {
    var vaildName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入用户名"));
      }
      setTimeout(() => {
        callback();
      }, 1000);
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      }
      setTimeout(() => {
        callback();
      }, 1000);
    };
    return {
      ruleForm: {
        name: "",
        pass: "",
        power: "",
      },
      rules: {
        name: [{ validator: vaildName, trigger: "blur" }],
        pass: [{ validator: validatePass, trigger: "blur" }],
        power: [
          {
            required: true,
            message: "请选择创建用户的权限",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName, form) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.login(form);
        }
      });
    },
    login(form) {
      var postForm = {
        name: form.name,
        password: form.pass,
        power: form.power === "普通用户" ? false : true,
      };

      this.$http
        .post("/server/user/login", postForm)
        .then((res) => {
          if (res.data.status) {
            window.sessionStorage.setItem(
              "userInfo",
              JSON.stringify(res.data.object)
            );
            this.$message({
              message: res.data.msg,
              type: "success",
            });
            this.$router.push("/airplane");
          } else {
            this.$message.error(res.data.msg);
          }
        });
    },
  },
};
</script>
<style>
.loginButton {
  margin: 10px;
}
.loginBox {
  height: 300px;
  width: 450px;
  border: 2px dashed #f69c55;
  margin: 200px auto;
  background-color: rgba(107, 206, 231, 0.8);
}
</style>
